package com.yiqi.liebang.feature.setting.view;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;

import com.google.gson.Gson;
import com.suozhang.framework.framework.BaseActivity;
import com.suozhang.framework.utils.T;
import com.umeng.socialize.UMAuthListener;
import com.umeng.socialize.UMShareAPI;
import com.umeng.socialize.UMShareConfig;
import com.umeng.socialize.bean.SHARE_MEDIA;
import com.yiqi.liebang.R;
import com.yiqi.liebang.entity.bo.WxUserBo;

import java.util.Map;

import butterknife.BindView;
import butterknife.ButterKnife;

public class BindingWeChatPay extends BaseActivity {
    @BindView(R.id.btn_weichat)
    Button btnWeichat;

    @Override
    protected int attachLayoutRes() {
        return R.layout.activity_binding_wechat;
    }
    UMShareAPI mShareAPI;
    WxUserBo mWxUserInfoBo;
    @Override
    protected void initInjector() {

    }

    @Override
    protected void initData() {
        UMShareConfig config = new UMShareConfig();
        config.isNeedAuthOnGetUserInfo(true);
        mShareAPI = UMShareAPI.get(this);
        mShareAPI.setShareConfig(config);
        btnWeichat.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                initUmLoginWx(SHARE_MEDIA.WEIXIN);
            }
        });
    }

    private void initUmLoginWx(final SHARE_MEDIA type) {
        try {
            if (!mShareAPI.isInstall(this, SHARE_MEDIA.WEIXIN)) {
                T.showShort("您未安装微信！");
                return;
            }
        } catch (Exception e) {
            showMsg(e.getMessage());
        }


        mShareAPI.doOauthVerify(this, type, new UMAuthListener() {
            @Override
            public void onStart(SHARE_MEDIA share_media) {

            }

            @Override
            public void onComplete(SHARE_MEDIA share_media, int i, Map<String, String> map) {


                mShareAPI.getPlatformInfo(BindingWeChatPay.this, type, new UMAuthListener() {
                    @Override
                    public void onStart(SHARE_MEDIA share_media) {

                    }

                    @Override
                    public void onComplete(SHARE_MEDIA share_media, int i, Map<String, String> map) {
                        if (map == null) return;
                        Gson gson = new Gson();
                        String s = gson.toJson(map);
                        mWxUserInfoBo = gson.fromJson(s, WxUserBo.class);
                        Intent intent=new Intent();
                        intent.putExtra("openId",mWxUserInfoBo.getOpenid());
                        setResult(30001,intent);
                        finish();

                    }

                    @Override
                    public void onError(SHARE_MEDIA share_media, int i, Throwable throwable) {
                        T.showShort(throwable.getMessage());
                    }

                    @Override
                    public void onCancel(SHARE_MEDIA share_media, int i) {

                    }
                });

            }

            @Override
            public void onError(SHARE_MEDIA share_media, int i, Throwable throwable) {

            }

            @Override
            public void onCancel(SHARE_MEDIA share_media, int i) {

            }
        });
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // TODO: add setContentView(...) invocation
        ButterKnife.bind(this);
    }
}
